package com.example.myapplication_02.pages;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication_02.R;
import com.example.myapplication_02.adapter.MsgAdapter;
import com.example.myapplication_02.entity.Msg;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
 private List<Msg> msgData = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
      Msg m1 = new Msg("你好",R.drawable.hashiqi,0);
        Msg m2 = new Msg("你好",R.drawable.fadou,1);
        Msg m3 = new Msg("干嘛呢",R.drawable.hashiqi,0);
        Msg m4 = new Msg("刚吃完饭",R.drawable.fadou,1);
        Msg m5 = new Msg("哦.好饱",R.drawable.hashiqi,0);

        msgData.add(m1);
        msgData.add(m2);
        msgData.add(m3);
        msgData.add(m4);
        msgData.add(m5);

        MsgAdapter adapter = new MsgAdapter(MainActivity.this,R.layout.litem_msg,msgData);
        ListView msgList = findViewById(R.id.list_msg);
        msgList.setAdapter(adapter);

        Button sendBtn = findViewById(R.id.btn_send);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText msgEdit = findViewById(R.id.edit_msg);
                String newContent = msgEdit.getText().toString();
                Msg newMsg = new Msg(newContent,R.drawable.hashiqi,0);
                msgData.add(newMsg);
                adapter.notifyDataSetChanged();
                msgList.smoothScrollByOffset(msgData.size()-1);
                msgEdit.setText("");

            }
        });

    }
}
